package com.haoshuang.zhouzhoubang.ui.page.mine.notice.member

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityPublishNoticeChooseMemberBinding
import com.haoshuang.zhouzhoubang.model.bean.GroupBean
import com.lnkj.jialubao.utils.AccountUtils
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.vertical

/**
 *  发布公告选择成员
 */
class PublishNoticeChooseMemberActivity :
    BaseVMActivity<PublishNoticeChooseMemberViewModel, ActivityPublishNoticeChooseMemberBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<PublishNoticeChooseMemberActivity>()
        }
    }

    private val data = arrayListOf<GroupBean.AllPeople>()
    private val adapter = PublishNoticeChooseMemberAdapter(data)

    private val ids by lazy { intent.getStringExtra("ids") }

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "选择师傅"
        binding.titleBar.leftImageView().click { finish() }

        binding.recyclerView.vertical().adapter = adapter
        adapter.setEmptyData()

        adapter.setOnItemClick { view, position, item ->
            item?.isSelected = !item!!.isSelected
            adapter.notifyItemChanged(position)
        }

        binding.tvConfirm.clickWithTrigger {
            val list = data.filter { it.isSelected }
            if (list.isEmpty()) {
                toast("请选择师傅")
                return@clickWithTrigger
            }
            val ids = list.joinToString(","){it.uid.toString()}
            val names = list.joinToString(","){it.nickname}
            setResult(RESULT_OK, Intent().apply {
                putExtra("ids", ids)
                putExtra("names", names)
            })
            finish()

        }
    }

    override fun initData() {
        vm.getTeamMemberList()
    }

    override fun startObserve() {
        vm.getTeamMemberListData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                data.clear()
                adapter.notifyDataSetChanged()
                adapter.isStateViewEnable = true
            }
            onSuccess {
                dismissLoading()
                val idList = ids?.split(",") ?: listOf()
                data.clear()
                data.addAll(it?: listOf())
                data.forEach {
                    it.isSelected = idList.contains(it.uid.toString())
                }
                adapter.notifyDataSetChanged()
                adapter.isStateViewEnable = true
            }
        }
    }

}